package com.li.o2oSystem.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.li.o2oSystem.dao.ShopDao;
import com.li.o2oSystem.dto.ImageHolder;
import com.li.o2oSystem.dto.ShopExecution;
import com.li.o2oSystem.entity.Shop;
import com.li.o2oSystem.enums.ShopStateEnum;
import com.li.o2oSystem.exceptions.ShopOperationException;
import com.li.o2oSystem.service.ShopService;
import com.li.o2oSystem.util.ImageUtil;
import com.li.o2oSystem.util.PageCalculator;
import com.li.o2oSystem.util.PathUtil;
/**
 * 
 * @author Administrator
 */
@Service
public class ShopServiceImpl implements ShopService {
	@Autowired
	private ShopDao shopDao;

	/**
	 * 添加店铺
	 */
	@Transactional
	public ShopExecution addShop(Shop shop,ImageHolder thumbnail) {
		//店铺空值判断
		if(shop == null){
			return new ShopExecution(ShopStateEnum.NULL_SHOP);//如果店铺为空，调用ShopExecution失败的构造函数
		}
		try{
			//给店铺信息赋初始值
			shop.setEnableStatus(0);//设置店铺状态
			shop.setCreateTime(new Date());//设置创建时间
			shop.setLastEditTime(new Date());//设置最后编辑时间
			//添加店铺信息
			int effectNum = shopDao.insertShop(shop);//插入店铺，返回影响行数
			if(effectNum <= 0){	//如果插入店铺的影响行数小于等于0
				throw new ShopOperationException("店铺创建失败！"); // 抛出运行时异常
			}else{
				if(thumbnail.getImage() != null){ //判断上传的图片是否为空
					//存储图片
					try{
						addShopImg(shop,thumbnail);
					}catch(Exception e){
						throw new ShopOperationException("添加图片失败:"+e.getMessage());
					}
					//更新图片地址
					effectNum = shopDao.updateShop(shop);//执行更新店铺方法，返回更新后影响行数
					if(effectNum <=0){
						throw new ShopOperationException("更新图片地址失败");
					}
				}
			}
		}catch(Exception e){
			throw new ShopOperationException("添加店铺失败："+e.getMessage());
		}
		return new ShopExecution(ShopStateEnum.CHECK,shop);
	}
	/**
	 * 添加图片
	 * @param shop
	 * @param shopImg
	 */
	private void addShopImg(Shop shop, ImageHolder thumbnail) {
		//获取shop图片目录的相对值路径
		String dest  = PathUtil.getShopImagePath(shop.getShopId());
		String shopImgAddr = ImageUtil.generateThumbnails(thumbnail,dest);//处理图片，并返回存储图片的地址
		shop.setShopImg(shopImgAddr);//设置店铺的地址
	}
	/**
	 * 根据店铺id查询店铺信息
	 */
	public Shop getByShopId(long shopId) {
		return shopDao.queryByShopId(shopId);
	}
	/**
	 * 修改商品信息，包括对图片的处理
	 */
	@Transactional
	public ShopExecution modifyShop(Shop shop, ImageHolder thumbnail)
			throws ShopOperationException {
		//判断店铺是否为空，或者店铺id是否为空
		if(shop == null||shop.getShopId() == null){
			return new ShopExecution(ShopStateEnum.NULL_SHOP);//满足为空状态，则返回店铺状态为空
		}else{
			try {
				//判断是否需要处理图片
				if(thumbnail.getImage() != null && thumbnail.getImageName() != null && !"".equals(thumbnail.getImageName())){
					Shop tempShop = shopDao.queryByShopId(shop.getShopId());//根据id查询店铺信息
					if(tempShop.getShopImg() != null){
						ImageUtil.deleteFileOrPath(tempShop.getShopImg());//删除编辑店铺的图片
					}
					addShopImg(shop,thumbnail);//添加店铺编辑后的图片
				}
				//更新店铺信息
				shop.setLastEditTime(new Date());
				int effectNum = shopDao.updateShop(shop);//执行更新店铺
				if(effectNum < 0){	
					return new ShopExecution(ShopStateEnum.INNER_ERROR);//如果影响行数小于0，则返回店铺状态，插入失败
				}else{
					shop = shopDao.queryByShopId(shop.getShopId());
					return new ShopExecution(ShopStateEnum.SUCCESS,shop);//返回店铺状态，操作成功
				}
			} catch (Exception e) {
				throw new ShopOperationException("modifyShop error:"+e.getMessage());
			}
		}
	}
	/**
	 * 根据shopCondition分页返回相应店铺列表数据
	 */
	public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
		//转换rowIndex(将页码装换成行码)
		int rowIndex = PageCalculator.calculatorRowIndex(pageIndex, pageSize);//将页码装换为行数
		List<Shop> shopList = shopDao.queryShopList(shopCondition, rowIndex, pageSize);//获取分页返回的店铺列表信息
		int count = shopDao.queryShopCount(shopCondition);//获取分页查询的店铺总数
		ShopExecution se = new ShopExecution();
		if(shopList != null){		//如果店铺列表信息不为空
			se.setShopList(shopList);//设置店铺列表
			se.setCount(count);	//设置店铺总数
		}else{
			se.setState(ShopStateEnum.INNER_ERROR.getState());//如果店铺列表为空，设置插入错误状态
		}
		return se;
	}
}
